package com.hstcs.systmes;

import java.util.Comparator;

import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;
import com.hstcs.assets.Config;
import com.hstcs.components.BoundsComponent;
import com.hstcs.components.BulletComponent;
import com.hstcs.components.HeroComponent;
import com.hstcs.components.MgComponent;
import com.hstcs.components.MonsterComponent;
import com.hstcs.components.PlComponent;
import com.hstcs.components.TextureComponent;
import com.hstcs.components.TransformComponent;


public class RenderingSystem extends IteratingSystem {
	static final float PIXELS_TO_METRES = 1.0f / 32.0f;
	
	private Batch batch;
	private Array<Entity> renderQueue;
	private Comparator<Entity> comparator;
	private OrthographicCamera cam;
	
	private ComponentMapper<TextureComponent> textureM;
	private ComponentMapper<TransformComponent> transformM;
	
	public ShapeRenderer shapes ;
	
	public RenderingSystem(Batch batch) {
		super(Family.one(PlComponent.class, TextureComponent.class).get());

		textureM = ComponentMapper.getFor(TextureComponent.class);
		transformM = ComponentMapper.getFor(TransformComponent.class);
		
		renderQueue = new Array<Entity>();
		
		comparator = new Comparator<Entity>() { 
			@Override
			public int compare(Entity entityB, Entity entityA) {
				return (int)Math.signum(transformM.get(entityB).pos.z -
										transformM.get(entityA).pos.z);
			}
		 };
		
		this.batch = batch;		
		cam = new OrthographicCamera(Config._WIDTH, Config._HEIGHT);
		cam.position.set(Config._WIDTH / 2, Config._HEIGHT / 2, 0);
	}

	@Override
	public void update(float deltaTime) {
		super.update(deltaTime);
		renderQueue.sort(comparator);
		
		cam.update();
		batch.setProjectionMatrix(cam.combined);
		batch.begin();
		for (Entity entity : renderQueue) {
			TextureComponent tex = textureM.get(entity);
			TransformComponent t = transformM.get(entity);

			//width:250.0 height:5.0orginX:125.0originY:2.5pos x:1280.0pos.y:720.0
			
			if(tex != null){
				if(tex.region == null) continue;
				float width = tex.region.getRegionWidth();
				float height = tex.region.getRegionHeight();
				float originX = width /2;
				float originY = height /2;
				batch.draw(tex.region, t.pos.x - originX, t.pos.y - originY,
						originX, originY, width, height, t.scale.x, t.scale.y,
						t.rotation);
				// MathUtils.radiansToDegrees * t.rotation);

			}else{
				PlComponent pl = entity.getComponent(PlComponent.class);
				pl.effect.draw(batch);
				//batch.disableBlending();
				
				System.out.println("sssssssssssssssssssssssssssssssssssssssssssssss");
				
			}
			
		}
		
		
		batch.end();
		renderQueue.clear();
	}
	
	@Override
	public void processEntity(Entity entity, float deltaTime) {
		renderQueue.add(entity);
	}
	
	public OrthographicCamera getCamera() {
		return cam;
	}
	public Batch batch(){
		return batch;
	}
}
